/* 
 倒计时
 
 页面内使用
 import { useCountDown } from '@/hooks/useCountDown.js'
 const { counter,startCountDown } = useCountDown()
 startCountDown(60)
 */

const useCountDown = () => {
  const counter = ref(0)
  const interval = ref(null)

  const startCountDown = num => {
    counter.value = Number(num)
    clearCountDown()
    interval.value = setInterval(() => {
      if (counter.value === 0) {
        clearInterval(interval.value)
        interval.value = null
        return
      }
      counter.value--
    }, 1000)
  }

  const clearCountDown = () => {
    if (interval.value) {
      clearInterval(interval.value)
    }
  }

  return { counter, startCountDown, clearCountDown }
}

export { useCountDown }